#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright [新大陆集成服务一部-系统组].
# Create Date:
# Update Date:
__authors__ = "新大陆集成服务一部-系统组"

"""4A数据文件导入程序.
将数据文件导入数据库.

执行结果说明:

时间戳: 执行时间
数据文件来源IP地址: 识别文件来源
执行结果状态:
    Local file UnComplete - 本地文件还未生成
    Miss local file[26_4Alog_20150205.dmp] - 本地文件缺失
    Oracle imp程序的执行结果 - 这个结果执行导入操作,必须检查执行结果

"""

import os
import sys
import datetime
from zipfile import *
import glob


class ImpData():
    def __init__(self):
        self.localdir = "/oracle/oracle/4a_nldata"
        self.iplist = ["23"]

        self.filesuffix = "4AData" + datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d") + ".zip"
        self.filelistsuffix = "4AData" + datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d")
        self.hisfilesuffix = "4AData" + datetime.datetime.strftime(datetime.datetime.now() - datetime.timedelta(days=7),
                                                                   "%Y%m%d")
        self.step = "4"
        self.prestep = "3"
        self.curcomplete = self.step + "complete" + str(
            datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d"))
        self.predaycurcomplete = self.step + "complete" + str(
            datetime.datetime.strftime(datetime.datetime.today() - datetime.timedelta(days=1), "%Y%m%d"))
        self.curputting = self.step + "putting" + str(datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d"))
        self.precomplete = self.prestep + "complete" + str(
            datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d"))
        self.predayprecomplete = self.prestep + "complete" + str(
            datetime.datetime.strftime(datetime.datetime.today() - datetime.timedelta(days=1), "%Y%m%d"))
        self.pid = os.path.join(self.localdir, str(datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d")))

        self.pid = os.path.join(self.localdir, str(datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d")))

        if not os.path.isfile(os.path.join(self.localdir, self.precomplete)):
            print "Pre step not complete !!!\n The Pre step file: " + os.path.join(self.localdir, self.precomplete)
            sys.exit()
        if os.path.isfile(self.pid):
            print "Have another process operation. Cancel operation!!!\n The pid file: " + self.pid
            sys.exit()
        if os.path.isfile(os.path.join(self.localdir, self.curcomplete)):
            print "Current step tasks for today already done.\n The flag file: " + os.path.join(self.localdir,
                                                                                                self.curcomplete)
            sys.exit()

        self.delfile()

        try:
            os.system("touch " + self.pid)
            self.imp()
        except Exception, e:
            print e
            sys.exit()
        finally:
            os.remove(self.pid)
            # os.system("touch " + os.path.join(self.localdir, self.curcomplete))

    def imp(self):
        try:
            # os.path.sep
            print "#" * 120
            print "#" + "Imp the 4ALog file to the Localhost".center(118) + "#"
            print "#" * 120
            ipnum = len(self.iplist)
            sucnum = 0
            for ip in self.iplist:
                print " "
                print "+" * 5 + str(datetime.datetime.now())
                print " " * 5 + "ip:" + ip
                completefile = os.path.join(self.localdir, self.precomplete + ip)
                # print completefile
                if os.path.isfile(completefile):
                    localfile = os.path.join(self.localdir, ip + self.filesuffix)
                    if os.path.isfile(localfile):
                        source_zfile = localfile
                        os.system(
                            "cd " + self.localdir + ";unzip -qj " + source_zfile + " -d " + self.localdir + "; rm -rf " + source_zfile)
                        for dmpfile in (
                                glob.glob(self.localdir + os.path.sep + ip + self.filelistsuffix + "*.dmp")):
                            cmd = "source ~/.bash_profile;imp nl4a/nl4a buffer=100000000 file=" + dmpfile + " full=y ignore=y log=" + dmpfile + "_imp.log"
                            os.system(cmd)
                            sucnum += 1
                    else:
                        print " " * 5 + ("Miss local file[" + localfile + "]")
                else:
                    print " " * 5 + "Local file UnComplete"
            if ipnum == sucnum:
                os.system("touch " + os.path.join(self.localdir, self.curcomplete))
        except Exception, e:
            os.remove(self.pid)
            print e
            sys.exit()
        finally:
            print " "
            print "#" * 120
            print "#" + "Finally".center(118) + "#"
            print "#" * 120
            datetime.timedelta()

    def delfile(self):
        for rmfile in glob.glob(os.path.join(self.localdir, self.predaycurcomplete)):
            os.remove(rmfile)
        for ip in self.iplist:
            for rmfile in glob.glob(os.path.join(self.localdir, ip + self.hisfilesuffix + "*")):
                os.remove(rmfile)
            for rmfile in glob.glob(os.path.join(self.localdir, self.predayprecomplete + "*")):
                os.remove(rmfile)


if __name__ == "__main__":
    do = ImpData()
